Page({
    data: {
      phoneNumber: '',
      password: '',
      phoneError: false
    },
  
    handlePhoneInput(e) {
      const value = e.detail.value;
      this.setData({
        phoneNumber: value,
        phoneError: false
      });
    },
  
    handlePasswordInput(e) {
      this.setData({
        password: e.detail.value
      });
    },
  
    validatePhone(phone) {
      const reg = /^1[3-9]\d{9}$/;
      return reg.test(phone);
    },
  
    handleLogin() {
      if (!this.validatePhone(this.data.phoneNumber)) {
        this.setData({ phoneError: true });
        wx.showToast({
          title: '请输入正确的手机号',
          icon: 'none'
        });
        return;
      }
  
      if (!this.data.password) {
        wx.showToast({
          title: '请输入密码',
          icon: 'none'
        });
        return;
      }
  
      // 模拟登录请求
      this.loginRequest();
    },
  
    loginRequest() {
      wx.showLoading({
        title: '登录中...',
      });
      
      // 模拟API请求
      setTimeout(() => {
        wx.hideLoading();
        
        // 模拟登录成功返回的数据
        const userInfo = {
          nickName: '用户_' + this.data.phoneNumber.slice(-4),
          phone: this.data.phoneNumber,
          memberLevel: '普通',
          points: 100,
          growth: 200,
          token: 'mock_token_' + Date.now()
        };
        
        // 存储用户信息
        wx.setStorageSync('userInfo', userInfo);
        wx.setStorageSync('token', userInfo.token);
        
        // 跳转到首页
        wx.switchTab({
          url: '/pages/index/index',
          success: () => {
            // 获取首页页面实例并更新数据
            const pages = getCurrentPages();
            if (pages.length > 0) {
              const indexPage = pages[0];
              if (indexPage.route === 'pages/index/index') {
                indexPage.onLoad();
              }
            }
          }
        });
      }, 1500);
    }
  });